package com.tanhua.manage.service;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.tanhua.manage.mapper.RetentionMapper;
import com.tanhua.manage.pojo.Retention;
import com.tanhua.manage.vo.RetentionVo;
import com.tanhua.manage.vo.UserTimeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class RetentionService {
    @Autowired
    private RetentionMapper retentionMapper;

    /**
     * 根据条件查询并保存信息
     */
    public List<RetentionVo> saveAndQueryAllRetention() throws ParseException {

        Date minDate = DateUtil.parse("2019-08-06");
        Date maxDate = DateUtil.parse("2020-01-01");

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Retention retention = new Retention();
        List<UserTimeVo> userTimeVosList = this.retentionMapper.queryRegister(minDate, maxDate);
        for (UserTimeVo userTimeVo : userTimeVosList) {
            //获取用户id、用户登录时间
            Long Id = userTimeVo.getId();
            Date registerTime = sf.parse(userTimeVo.getCreated());
            //根据id查用户登录时间
            List<Date> logTimeList = this.retentionMapper.queryLogTime(Id);
            Integer sex = this.retentionMapper.querySex(Id);
            //注册时间和登录时间的天数差
            for (Date logTime : logTimeList) {
                if (registerTime != null && logTime != null) {
                    Long days = DateUtil.between(registerTime, logTime, DateUnit.DAY);


                    //封装用户id，登录时间，注册时间,时间差days
                    retention.setUserId(Id);
                    retention.setSex(sex);
                    retention.setLogTime(DateUtil.format(logTime, "yyyy-MM-dd"));
                    retention.setRegisterTime(DateUtil.format(registerTime, "yyyy-MM-dd"));
                    retention.setDays(days);
                    //存入retention
                    this.retentionMapper.insertRetention(retention);
                }
            }
        }
        List<RetentionVo> allRetention = this.getAllRetention();
        return allRetention;
    }

    /**
     * 获取全部retentionVo对象,计算留存
     */
    public List<RetentionVo> getAllRetention() {
//        Date minDate = this.retentionMapper.queryMinRegisterTime();
//        Date maxDate = this.retentionMapper.queryMaxRegisterTime();
        String minDate = "2019-08-06";
        String maxDate = "2020-01-01";
        Integer sex = null;
//        Date minDate = DateUtil.parse("2019-08-06");
//        Date maxDate = DateUtil.parse("2020-01-01");
        return this.getPartRetention(minDate, maxDate,sex);
    }


    /**
     * 获取筛选retentionVo对象,计算留存
     */
    public List<RetentionVo> getPartRetention(String start, String end, Integer sex) {//   Integer sex
        Date sd = DateUtil.parse(start);
        Date ed = DateUtil.parse(end);
        List<RetentionVo> retentionVoList = new ArrayList<>();
//        Format format = new SimpleDateFormat("yyyy-MM-dd");
        while (!(sd.compareTo(ed) == 1)) {
            RetentionVo retentionVo = new RetentionVo();
            //得到注册人数
            Integer numRegister = this.retentionMapper.queryNumRegister(sd);
            //避免注册人数为零
            if (numRegister == 0) {
                retentionVo.setDataTime(sd);
                retentionVo.setRegisterNum(0);
                retentionVo.setOnedayLater("0.00%");
                retentionVo.setTwodayLater("0.00%");
                retentionVo.setThreedayLater("0.00%");
                retentionVo.setFourdayLater("0.00%");
                retentionVo.setFivedayLater("0.00%");
                retentionVo.setSixdayLater("0.00%");
                retentionVo.setSevendayLater("0.00%");
                retentionVo.setMonthdayLater("0.00%");
                continue;
            }
            int numLog = 0;
            //计算1-7天后的留存率
            for (int i = 1; i <= 7; i++) {
                //得到i天后的登录人数
                if (sex!=null) {
                    numLog = this.retentionMapper.queryRetentionWithSex(DateUtil.format(sd, "yyyy-MM-dd"), i, sex);
                }else{
                    numLog = this.retentionMapper.queryRetention(DateUtil.format(sd, "yyyy-MM-dd"), i);

                }
                //封装日期，注册人数到RetentionVo
                retentionVo.setDataTime(sd);
                retentionVo.setRegisterNum(numRegister);

                //计算留存率
                String result;

                // 创建一个数值格式化对象
                NumberFormat nf = NumberFormat.getInstance();
                // 设置精确到小数点后2位
                nf.setMaximumFractionDigits(2);
                result = nf.format((float) numLog / (float) numRegister * 100);

                //判断i并存入相应位置
                switch (i) {
                    case 1:
                        retentionVo.setOnedayLater(result + "%");
                        break;
                    case 2:
                        retentionVo.setTwodayLater(result+ "%");
                        break;
                    case 3:
                        retentionVo.setThreedayLater(result+ "%");
                        break;
                    case 4:
                        retentionVo.setFourdayLater(result+ "%");
                        break;
                    case 5:
                        retentionVo.setFivedayLater(result+ "%");
                        break;
                    case 6:
                        retentionVo.setSixdayLater(result+ "%");
                        break;
                    case 7:
                        retentionVo.setSevendayLater(result+ "%");
                        break;
                    default:
                        break;
                }
            }

            //计算30天后的留存率
            //得到30天后的登录人数
            if (sex!= null) {
                numLog = this.retentionMapper.queryRetentionWithSex(DateUtil.format(sd, "yyyy-MM-dd"), 30, sex);
            }else{
                numLog = this.retentionMapper.queryRetention(DateUtil.format(sd, "yyyy-MM-dd"), 30);
            }
            //计算留存率
            String result;
            //避免注册人数为零

            // 创建一个数值格式化对象
            NumberFormat nf = NumberFormat.getInstance();
            // 设置精确到小数点后2位
            nf.setMaximumFractionDigits(2);
            result = nf.format((float) numLog / (float) numRegister * 100);
            retentionVo.setMonthdayLater(result + "%");
            retentionVoList.add(retentionVo);

            //使日期增加
            Calendar c = Calendar.getInstance();
            c.setTime(sd);
            c.add(Calendar.DAY_OF_MONTH, 1);// 今天+1天
//            sd = c.getTime();

            String sdd = DateUtil.format(c.getTime(), "yyyy-MM-dd"); //格式化成yyyy-MM-dd格式的时间字符串
            sd =DateUtil.parse(sdd);
        }
        return retentionVoList;
    }



}
